package socialnetworks;

import java.io.*;

import java.net.*;
import java.util.*;

import com.google.gdata.client.*;
import com.google.gdata.client.youtube.*;
import com.google.gdata.data.*;
import com.google.gdata.data.geo.impl.*;
import com.google.gdata.data.media.*;
import com.google.gdata.data.media.mediarss.*;
import com.google.gdata.data.youtube.*;
import com.google.gdata.data.extensions.*;
import com.google.gdata.util.*;

public class YouTubeDataExtractor 
{	protected YouTubeService service=null;
	
	public YouTubeDataExtractor()
	{	service = new YouTubeService("YTDumpTetris","AI39si66s0Wd9UaEY60Q2ZGltxjClSzQsx37zyYHb_hZdhSp4oemIgiT20rnClizB7jWQYbuwpw5Vs9bWi2O5_-bHd5NjgHKlA");
	}

	public String utente(String idVideo) throws ServiceException, IOException
	{	VideoEntry videoEntry = service.getEntry(new URL("http://gdata.youtube.com/feeds/api/videos/"+idVideo), VideoEntry.class);
		return videoEntry.getMediaGroup().getUploader();
	}
	
	public List<String> videoCaricati(String userid) throws MalformedURLException, IOException, ServiceException
	{	List<String> l=new ArrayList<String>();
		VideoFeed videoFeed = service.getFeed(new URL("http://gdata.youtube.com/feeds/api/users/"+userid+"/uploads"), VideoFeed.class);
		for(VideoEntry videoEntry : videoFeed.getEntries() ) 
			l.add(videoEntry.getMediaGroup().getVideoId());
		return l;
	}
	
	public List<String> videoPreferiti(String userid) throws MalformedURLException, IOException, ServiceException
	{	List<String> l=new ArrayList<String>();
		VideoFeed videoFeed = service.getFeed(new URL("http://gdata.youtube.com/feeds/api/users/"+userid+"/favorites"), VideoFeed.class);
		for(VideoEntry videoEntry : videoFeed.getEntries() ) 
			l.add(videoEntry.getMediaGroup().getVideoId());
		return l;
	}
	
	public List<String> videoCorrelati(String idVideo) throws MalformedURLException, IOException, ServiceException
	{	List<String> l=new ArrayList<String>();
		VideoEntry videoEntry = service.getEntry(new URL("http://gdata.youtube.com/feeds/api/videos/"+idVideo), VideoEntry.class);
		if (videoEntry.getRelatedVideosLink()!=null) 
		{	VideoFeed videoFeed = service.getFeed(new URL(videoEntry.getRelatedVideosLink().getHref()), VideoFeed.class);
			for(VideoEntry rVideoEntry : videoFeed.getEntries() ) 
				l.add(rVideoEntry.getMediaGroup().getVideoId());
		}
		return l;
	}
	
	public List<String> query(String q) throws ServiceException, IOException
	{	YouTubeQuery query = new YouTubeQuery(new URL("http://gdata.youtube.com/feeds/api/videos"));
		query.setFullTextQuery(q);
		VideoFeed videoFeed = service.query(query, VideoFeed.class);
		List<String> risultato=new ArrayList<String>();
		for(VideoEntry videoEntry : videoFeed.getEntries()) 
			risultato.add(videoEntry.getMediaGroup().getVideoId());
		return risultato;
	}
	
	public String titolo(String idVideo) throws ServiceException, IOException
	{	VideoEntry videoEntry = service.getEntry(new URL("http://gdata.youtube.com/feeds/api/videos/"+idVideo), VideoEntry.class);
		String titolo="";
		if(videoEntry!=null)
			titolo=videoEntry.getTitle().getPlainText();
		return titolo;
	}

	public List<String> paroleChiave(String idVideo) throws ServiceException, IOException
	{	VideoEntry videoEntry = service.getEntry(new URL("http://gdata.youtube.com/feeds/api/videos/"+idVideo), VideoEntry.class);
		YouTubeMediaGroup mediaGroup = videoEntry.getMediaGroup();
		return mediaGroup.getKeywords().getKeywords();		
	}
}
